package com.authManager.server.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

import com.authManager.server.service.ISysUserRoleService;
import com.authManager.server.dao.SysUserRoleMapper;
import com.authManager.entity.SysUserRole;

import com.commons.base.model.AdminPage;
import com.commons.base.model.ResultCode;
import com.commons.base.model.ResultData;

/**
 * <p>
 * 【用户角色关联表】 服务实现类
 * </p>
 *
 * @author 小旋风
 * @since 2018-8-17
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SysUserRoleServiceImpl.class);
	@Transactional
	@Override
	public ResultData<Object> addOrDelData(Long sysRoleId, String sysUserId,String creater) {
		SysUserRole sysUserRole = new SysUserRole().setSysUserId(sysUserId).setSysRoleId(sysRoleId);
		int cout = this.selectCount(new EntityWrapper<>(sysUserRole));
		if(cout>0) {
			this.baseMapper.delete(new EntityWrapper<>(sysUserRole));
		}else {
			sysUserRole.setCreater(creater);
			sysUserRole.setCreateTime(new Date());
			this.baseMapper.insert(sysUserRole);
		}
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}
	@Override
	public ResultData<AdminPage<SysUserRole>> listSysUserRole(Map<String, Object> paramMap) {
		Page<SysUserRole> page = new Page<>(Integer.valueOf(String.valueOf(paramMap.get("pageNum"))),
				Integer.valueOf(String.valueOf(paramMap.get("pageSize"))));
		Wrapper<SysUserRole> entityWrapper = new EntityWrapper<>();

		// 主键id
		if (paramMap.get("sysUserRoleId") != null) {
			entityWrapper.eq("sys_user_role.sys_user_role_id", paramMap.get("sysUserRoleId"));
		}
		// 用户id
		if (paramMap.get("sysUserId") != null) {
			entityWrapper.like("sys_user_role.sys_user_id", String.valueOf(paramMap.get("sysUserId")));
		}
		// 角色id
		if (paramMap.get("sysRoleId") != null) {
			entityWrapper.eq("sys_user_role.sys_role_id", paramMap.get("sysRoleId"));
		}
		// 开始-创建时间
		if (paramMap.get("start_createTime") != null) {
			entityWrapper.le("sys_user_role.create_time", paramMap.get("start_createTime"));
		}
		// 结束-创建时间
		if (paramMap.get("end_createTime") != null) {
			entityWrapper.le("sys_user_role.create_time", paramMap.get("end_createTime"));
		}
		this.selectPage(page, entityWrapper);
		return new ResultData.Builder<AdminPage<SysUserRole>>(ResultCode.success)
				.setData(new AdminPage<SysUserRole>(page)).build();
	}
}
